package com.iptv.grape.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;

import com.iptv.grape.util.AppLog;

/****
 *     tvScroll.startScroll();开始滚动
 *     tvScroll.stopScroll(); 停止滚动
 */
@SuppressLint("AppCompatCustomView")
public class MarqueeTextView extends TextView implements Runnable {
    private int currentScrollX; // 当前滚动的位置
    private boolean isStop = false;
    private int textWidth;
    private boolean isMeasure = false;

    public MarqueeTextView(Context context) {
        super(context);
        this.setEllipsize(TextUtils.TruncateAt.END);
    }

    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setEllipsize(TextUtils.TruncateAt.END);
    }

    public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.setEllipsize(TextUtils.TruncateAt.END);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        currentScrollX = this.getWidth();
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!isMeasure) {
            getTextWidth();// 文字宽度只需要获取一次就可以了
            isMeasure = true;
        }
    }

    private void getTextWidth() {
        Paint paint = this.getPaint();
        String str = this.getText().toString();
        textWidth = (int) paint.measureText(str);
    }

    @Override
    public void run() {
        currentScrollX += 1;// 滚动速度.+号表示往左边-
        scrollTo(currentScrollX, 0);
        if (isStop) {
            return;
        }
        if (getScrollX() >= (textWidth)) {
            currentScrollX = -(this.getWidth());// 当前出现的位置
        }
        postDelayed(this, 200);
    }

    //        tv.setEllipsize(TruncateAt.valueOf("END"));
//
//tv.setEllipsize(TruncateAt.valueOf("START"));
//
//tv.setEllipsize(TruncateAt.valueOf("MIDDLE"));
//
//tv.setEllipsize(TruncateAt.valueOf("MARQUEE"));
    // 开始滚动
    public void startScroll() {
        Paint paint = new Paint();
        paint.setTextSize(this.getTextSize());
        int size = (int) paint.measureText(this.getText().toString());
        int width = this.getMeasuredWidth();
        if (size > width) {
            this.setEllipsize(TextUtils.TruncateAt.MARQUEE);
            isStop = false;
            this.removeCallbacks(this);
            post(this);
        }
    }

    // 停止滚动
    public void stopScroll() {
//        currentScrollX = 0;
//        isStop = true;
//        this.removeCallbacks(this);
//        this.setEllipsize(TextUtils.TruncateAt.END);
        isStop = true;
        this.removeCallbacks(this);
        currentScrollX = 0;
        this.setEllipsize(TextUtils.TruncateAt.END);
        scrollTo(currentScrollX, 0);



    }

    // 从头开始滚动
    public void startFromHead() {
        currentScrollX = 0;
        startScroll();
    }

}
